/**
 * 
 */
package pl.com.ozzy.ozsign.tools;

import pl.com.ozzy.ozsign.ui.MainUI;
import pl.com.ozzy.ozsign.ui.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
 *
 */
public class SignService extends Service {

	private static final int NOTIFICATION = 0;
	private final IBinder mBinder = new SignBinder();
	private NotificationManager mNotificationManager;

	public class SignBinder extends Binder {
		SignService getService() {
			return SignService.this;
		}
	}

	
	@Override
	public IBinder onBind(Intent arg0) {
		return mBinder;
	}

	
	@Override
    public void onCreate() {
        mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
    }
	
	
	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }
	
	@Override
	public void onDestroy() {
		// Cancel the persistent notification.
		mNotificationManager.cancel(NOTIFICATION);

		// Tell the user we stopped.
		Toast.makeText(this, R.string.msg_service_stopped, Toast.LENGTH_SHORT)
				.show();
	}
	
	
	/**
     * Show a notification while this service is running.
     */
    private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.msg_service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(android.R.drawable.star_on, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainUI.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.label_service),
                       text, contentIntent);

        // Send the notification.
        mNotificationManager.notify(NOTIFICATION, notification);
    }
}
